package com.example.hrd.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.agp.utils.TextAlignment;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.concurrent.atomic.AtomicInteger;

public class SecondAbilitySlice extends AbilitySlice {
    HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG");
    private DirectionalLayout layout;
    private int[][] grids;
    private AtomicInteger row_0;
    private AtomicInteger column_0;

    @Override
    public void onStart(Intent intent) {

        super.onStart(intent);

        initialize();

    }

    public void initialize() {
        row_0 = new AtomicInteger(1);
        column_0 = new AtomicInteger(2);
        layout = new DirectionalLayout(this);
        grids = new int[][]{{9, 5, 4, 12}, {14, 6, 0, 8,}, {1, 10, 3, 13}, {15, 7, 11, 2}};

        createGrids(grids);
        //slideGrids();
        drawGrids(grids);
        drawButton();
    }

    public void createGrids(int[][] grids) {
        int[] array = {-1, -2, 1, 2};

        for (int i = 0; i < 100; i++) {
            int random = (int) Math.floor(Math.random() * 4);
            int direction = array[random];
            changeGrids(grids, direction);
        }
        if (gameOver()) {
            createGrids(this.grids);
        }
    }

    public void drawGrids(int[][] grids) {
        layout.setLayoutConfig((new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT)));
        long one = System.currentTimeMillis();
        Component.DrawTask task = (component, canvas) -> {
            Paint mPaint = new Paint();

            mPaint.setColor(Color.GRAY);
            RectFloat rect = new RectFloat(2, 230, 1078, 1306);
            canvas.drawRect(rect, mPaint);

            for (int row = 0; row < 4; row++) {
                for (int column = 0; column < 4; column++) {
                    mPaint.setColor(Color.CYAN);
                    RectFloat rectFloat = new RectFloat(22 + column * 262, 250 + row * 262, 272 + column * 262, 500 + row * 262);
                    canvas.drawRect(rectFloat, mPaint);

                    mPaint.setColor(Color.YELLOW);
                    mPaint.setTextSize(125);
                    if (grids[row][column] != 0) {
                        if (grids[row][column] < 10) {
                            canvas.drawText(mPaint, String.valueOf(grids[row][column]), 105 + column * 262, 425 + row * 262);
                        } else {
                            canvas.drawText(mPaint, String.valueOf(grids[row][column]), 65 + column * 262, 425 + row * 262);
                        }
                    }
                }
            }
        };
        layout.addDrawTask(task);
        setUIContent(layout);
        if (gameOver()) {
            drawText();
        }
        long two = System.currentTimeMillis();
        HiLog.info(label, "绘图耗时%{public}d", (two - one));
    }

    public void drawButton() {

        Button button = new Button(this);
        button.setText("重新开始");
        button.setTextSize(100);
        button.setTextAlignment(TextAlignment.CENTER);
        button.setTextColor(Color.WHITE);
        button.setMarginTop(1400);
        button.setMarginLeft(80);
        button.setPadding(20, 20, 20, 20);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(new RgbColor(174, 158, 143));
        background.setCornerRadius(100);
        button.setBackground(background);
        button.setClickedListener(component -> initialize());
        layout.addComponent(button);

        Button button0 = new Button(this);
        button0.setText("返回");
        button0.setTextSize(100);
        button0.setTextAlignment(TextAlignment.CENTER);
        button0.setTextColor(Color.WHITE);
        button0.setMarginTop(-170);
        button0.setMarginLeft(680);
        button0.setPadding(20, 20, 20, 20);
        button0.setBackground(background);
        button0.setClickedListener(component -> present(new SecondAbilitySlice(), new Intent()));
        layout.addComponent(button0);


        ShapeElement background0 = new ShapeElement();
        background0.setRgbColor(new RgbColor(174, 158, 143));
        background0.setCornerRadius(100);

        Button button1 = new Button(this);
        button1.setText("↑");
        button1.setTextAlignment(TextAlignment.CENTER);
        button1.setTextColor(Color.WHITE);
        button1.setTextSize(100);
        button1.setMarginLeft(500);
        button1.setMarginTop(70);
        button1.setPadding(12, 0, 12, 0);
        button1.setBackground(background0);
        button1.setClickedListener(component -> {
            HiLog.info(label, "上");
            changeGrids(grids, 2);
        });
        layout.addComponent(button1);

        Button button2 = new Button(this);
        button2.setText("←");
        button2.setTextAlignment(TextAlignment.CENTER);
        button2.setTextColor(Color.WHITE);
        button2.setTextSize(100);
        button2.setMarginTop(10);
        button2.setMarginLeft(400);
        button2.setPadding(12, 0, 12, 0);
        button2.setBackground(background0);
        button2.setClickedListener(component -> {
            HiLog.info(label, "左");
            changeGrids(grids, -1);
        });
        layout.addComponent(button2);

        Button button3 = new Button(this);
        button3.setText("→");
        button3.setTextAlignment(TextAlignment.CENTER);
        button3.setTextColor(Color.WHITE);
        button3.setTextSize(100);
        button3.setMarginLeft(600);
        button3.setMarginTop(-130);
        button3.setPadding(12, 0, 12, 0);
        button3.setBackground(background0);
        button3.setClickedListener(component -> {
            HiLog.info(label, "右");
            changeGrids(grids, 1);
        });
        layout.addComponent(button3);

        Button button4 = new Button(this);
        button4.setText("↓");
        button4.setTextAlignment(TextAlignment.CENTER);
        button4.setTextColor(Color.WHITE);
        button4.setTextSize(100);
        button4.setMarginLeft(500);
        button4.setMarginTop(10);
        button4.setPadding(12, 0, 12, 0);
        button4.setBackground(background0);
        button4.setClickedListener(component -> {
            HiLog.info(label, "下");
            changeGrids(grids, -2);
        });
        layout.addComponent(button4);

    }

    public void changeGrids(int[][] grids, int direction) {
        int temp;
        boolean isDrawGrids = false;
        HiLog.info(label, "当前坐标(%{public}d,%{public}d)", row_0.get(), column_0.get());
        if (direction == -1 && (column_0.get() + 1) <= 3) {
            temp = grids[row_0.get()][column_0.get() + 1];
            grids[row_0.get()][column_0.get() + 1] = grids[row_0.get()][column_0.get()];
            grids[row_0.get()][column_0.get()] = temp;
            column_0.incrementAndGet();
            isDrawGrids = true;
        } else if (direction == 1 && (column_0.get() - 1) >= 0) {
            temp = grids[row_0.get()][column_0.get() - 1];
            grids[row_0.get()][column_0.get() - 1] = grids[row_0.get()][column_0.get()];
            grids[row_0.get()][column_0.get()] = temp;
            column_0.decrementAndGet();
            isDrawGrids = true;
        } else if (direction == 2 && (row_0.get() + 1) <= 3) {
            temp = grids[row_0.get() + 1][column_0.get()];
            grids[row_0.get() + 1][column_0.get()] = grids[row_0.get()][column_0.get()];
            grids[row_0.get()][column_0.get()] = temp;
            row_0.incrementAndGet();
            isDrawGrids = true;
        } else if (direction == -2 && (row_0.get() - 1) >= 0) {
            temp = grids[row_0.get() - 1][column_0.get()];
            grids[row_0.get() - 1][column_0.get()] = grids[row_0.get()][column_0.get()];
            grids[row_0.get()][column_0.get()] = temp;
            row_0.decrementAndGet();
            isDrawGrids = true;
        }
        HiLog.info(label, "移动后坐标(%{public}d,%{public}d)", row_0.get(), column_0.get());
        if (isDrawGrids) {
            drawGrids(this.grids);
        }
    }

    public boolean gameOver() {
        long one = System.currentTimeMillis();
        if (row_0.get() == 3 && column_0.get() == 3) {
            int[][] gameOverGrids = {{1, 2, 3, 4}, {5, 6, 7, 8,}, {9, 10, 11, 12}, {13, 14, 15, 0}};
            for (int row = 0; row < 4; row++) {
                for (int column = 0; column < 4; column++) {
                    if (grids[row][column] != gameOverGrids[row][column]) {
                        long two = System.currentTimeMillis();
                        HiLog.info(label, "判断结束耗费时长%{public}d", (two - one));
                        return false;
                    }
                }
            }
            long two = System.currentTimeMillis();
            HiLog.info(label, "判断结束耗费时长%{public}d---%{public}s", (two - one), "成功");
            return true;
        }
        return false;
    }

    public void drawText() {
        Text text = new Text(this);
        text.setText("游戏成功");
        text.setTextSize(100);
        text.setTextColor(Color.BLUE);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setMarginsTopAndBottom(-2000, 0);
        text.setMarginsLeftAndRight(350, 0);
        layout.addComponent(text);
        setUIContent(layout);

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
